package com.study.aliyun.datawork;

import com.study.util.FileUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @version V1.0
 * @title: DBUtil
 * @description: ${DESCRIPTION}
 * @author: wanghuilong
 * @date: 2020-06-10 18:03
 * @copyright: http://fingo.com.my/
 */
public class MainDBUtil {

//    // MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
//    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//    static final String DB_URL = "jdbc:mysql://localhost:3306/RUNOOB";

    //MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://rm-ks-qa.mysql.rds.aliyuncs.com/information_schema?useSSL=false&serverTimezone=UTC";

    // 数据库的用户名与密码，需要根据自己的设置
    static final String USER = "qa";
    static final String PASS = "NTQ0NjU5YjU0";

    static Connection conn = null;
    static Statement stmt = null;
    static ResultSet rs = null;

    static String seport = "\t";

    public static void init() {
        try {
            // 注册 JDBC 驱动
            Class.forName(JDBC_DRIVER);

            // 打开链接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
            // 执行查询
            System.out.println(" 实例化Statement对象...");
            stmt = conn.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static List<String> tables = Arrays.asList("gnu_tag");

    static List<String> checkSql = new ArrayList<>();

    static List<String> dropTableSql = new ArrayList<>();

    public static void main(String[] args) {
        init();
        List<String> list = new ArrayList<>();

        for (int i = 0; i < tables.size(); i++) {
            String tableName = tables.get(i);
            try {
                String sql = "SELECT table_name, table_comment FROM tables" +
                        " where TABLE_NAME = '" + tableName + "' and TABLE_SCHEMA = 'share-product'";
                rs = stmt.executeQuery(sql);
                boolean key = false;
                // 展开结果集数据库
                while (rs.next()) {
                    // 通过字段检索
                    String table_name = rs.getString(1);
                    String table_comment = rs.getString(2);
                    String ods = createOdsTableSql(tableName, table_comment, "ods", "df");
                    list.add(ods);
                    key = true;
                }
                if (!key) {
                    System.out.println("没有查到数据：" + tableName);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        close();
        FileUtil.writeToFileByLine(list, "src/main/resources/file/table.sql");
        System.out.println("tables:" + tables.size() + ",sqlsize:" + list.size());
        for (int i = 0; i < checkSql.size(); i++) {
            System.out.println(checkSql.get(i));
        }

        for (int i = 0; i < dropTableSql.size(); i++) {
            System.out.println(dropTableSql.get(i));
        }
    }

    public static String createOdsTableSql(String tableName, String tableComment, String prefix, String stuffix) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(tableName).append(seport).append(tableComment).append("\n");
        try {
            String sql = "select COLUMN_NAME, DATA_TYPE AS `数据类型`,COLUMN_COMMENT from COLUMNS where " +
                    "TABLE_NAME = '" + tableName + "' and TABLE_SCHEMA = 'share-product'";
            rs = stmt.executeQuery(sql);
            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                String colName = rs.getString(1);
                String type = rs.getString(2);
                String content = rs.getString(3);
                stringBuilder.append(colName).append(seport).append(type).append(seport).append(content).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public static void close() {
        try {
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
        System.out.println("Goodbye!");
    }
}
